﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using SharePointOfView.Diagnostics;

namespace SharePointOfView.EventReceivers
{
    /// <summary>
    /// This Feature Receiver will activate all the features specified in its properties. 
    /// see http://blogs.developpeur.org/phil/archive/2008/12/01/sharepoint-2007-activation-de-features-et-activationdependencies.aspx
    /// for more information.
    /// </summary>
    public class ActivationFeatureReceiver : SPFeatureReceiver
    {
        private const string LOGCATEGORY = "SharePointOfView";

        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            try
            {
                foreach (SPFeatureProperty featureProp in properties.Feature.Properties)
                {
                    Guid featureGuid = new Guid(featureProp.Value);
                    site.Features.Add(featureGuid, properties.Definition.AlwaysForceInstall);
                }
            }
            catch (Exception ex)
            {
                ULS.WriteError("Error activating Features. Message : " + ex.Message, LOGCATEGORY);
                ULS.WriteError("Rolling back...", LOGCATEGORY);

                site.Features.Remove(properties.Feature.DefinitionId);
            }
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            try
            {
                for (int i = properties.Feature.Properties.Count - 1; i >= 0; i--)
                {
                    SPFeatureProperty featureProp = properties.Feature.Properties[i] as SPFeatureProperty;

                    Guid featureGuid = new Guid(featureProp.Value);

                    SPFeature feature = site.Features[featureGuid];

                    if (feature != null)
                    {
                        site.Features.Remove(featureGuid);
                    }
                }
            }
            catch (Exception ex)
            {
                ULS.WriteError("Error de-activating Features. Message : " + ex.Message, LOGCATEGORY);
                throw ex;
            }
        }

        #region Not Used
        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
        #endregion
    }
}
